package com.agu.mgt.sys.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.alibaba.fastjson.JSONObject;
import com.agu.common.excel.Column;
import com.agu.common.excel.ExcelExport;
import com.agu.common.excel.ExcelUtil;
import com.agu.entity.sys.SysUserEntity;
import com.agu.utils.ShiroUtils;

/**
 * Controller公共组件
 * 
 */
public abstract class AbstractController {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	protected SysUserEntity getUser() {
		return ShiroUtils.getUserEntity();
	}

	protected Long getUserId() {
		return getUser().getUserId();
	}
	
	protected Long getSpId() {
		return getUser().getSpId();
	}
	
	protected Long getNowTime() {
		return System.currentTimeMillis()/1000;
	}
	
	protected Integer getProvinceId() {
		return getUser().getProvinceId();
	}
	
	protected Integer getUserType() {
		return getUser().getUserType();
	}
	
	//下载压缩文件
	protected void downzip(ExcelExport export,HttpServletResponse response) {
		try {
			response.reset();
			response.setHeader("Content-Disposition", "attachment; filename=\"data.zip\"");
			response.setContentType("application/octet-stream; charset=UTF-8");
			
			OutputStream out = response.getOutputStream();  
	        
	        ZipOutputStream zipOutputStream = new ZipOutputStream(out); 
			
			ZipEntry entry = new ZipEntry("data.xls");  
			zipOutputStream.putNextEntry(entry);  
	        export.WriteExcel(zipOutputStream);  
	        
	        // 关闭输出流  
	         zipOutputStream.flush();  
	         zipOutputStream.close();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}
	//下载excel文件
	protected void downExcel(ExcelExport export,HttpServletResponse response) {
		try {
			response.reset();
			response.setHeader("Content-Disposition", "attachment; filename=\"data.xls\"");
			response.setContentType("application/octet-stream; charset=UTF-8");
			export.WriteExcel(response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	protected ExcelExport exportList(List list, List<Column> clist, Map map) {

		String sheetName = "数据";

		if (list != null && list.size() > 0) {

			if (clist == null) {
				return null;
			}

			ExcelExport xls = new ExcelExport(true, sheetName);
			xls.createRow(1); // 输入标题栏
			int j = 1;
			int i = 2;
			// xls.createStyle1();

			for (Column col : clist) {
				xls.setTitleCell(j++, col.getName());
			}

			for (Object o : list) {
				xls.createRow(i++);
				j = 1;

				boolean b = o instanceof java.util.Map;

				for (Column col : clist) {
					int coltype = col.getType();
					String colname = col.getField();
					Object value = null;
					if (b) {
						value = ((Map) o).get(colname);
					} else {
						value = ExcelUtil.getMethodValue(o, colname);
					}

					switch (coltype) {
					case 1: // 正常值
						xls.setCell(j++, value);
						break;
					case 2: // map;
						if (map == null) {
							xls.setCell(j++, "");
							break;
						}
						Object mo = map.get(col.getField()+"Map");
						if (mo == null) {
							xls.setCell(j++, "");
							break;
						}
						Map m = (Map) mo;
						Object mv = m.get(value);
						if (mv == null) {
							xls.setCell(j++, "");
							break;
						}
						xls.setCell(j++, mv);
						break;
					case 3: // 日期
						if (!StringUtils.isBlank(col.getDateFormat())) {
							xls.setDateCell(j++, value, col.getDateFormat());
						} else {
							xls.setDateCell(j++, value);
						}
						break;
					default:
						xls.setCell(j++, value);
					}
				}
			}

			xls.setColumnWidth(0, 500);
			j = 1;
			for (Column col : clist) {
				xls.setColumnAutoWidth(j++);
				//xls.setColumnWidth(j++, 5500);
			}

			return xls;
		} else {
			return null;
		}
	}
}
